﻿using _01_Tower4._0.GameFrame.Manager;
using _01_Tower4._0.GameFrame.System;

namespace _01_Tower4._0.GameFrame
{
    internal class Game
    {
        public enum Scene
        {
            Attack = 0,
            MainMenu,
            Map1,
            ToolMenu
        }

        public static bool isOver;
        bool isPause;
        long lastTime, fixedTime, currentTime;
        int speed, fps;
        public static Random rand;

        string[] sceneNames;

        public Game()
        {
            rand = new Random();

            sceneNames = new string[] { "SceneMenu", "SceneMap1", "SceneCombat", "SceneBag", "SceneKingDialog", "SceneMap2" };
        }

        public void Play()
        {
            Init();
            fixedTime = DateTime.Now.Ticks;
            lastTime = DateTime.Now.Ticks;
            while (!isOver)
            {
                InputSystem.Instance.Update();
                Input();
                // 游戏的暂停其实是逻辑的暂停 按键检测和画面更新不能停
                if (!isPause)
                {
                    Update();
                }
                currentTime = DateTime.Now.Ticks;
                if (currentTime - fixedTime > 10_000_000 / speed)
                {
                    if (!isPause)
                    {
                        FixedUpdate();
                    }
                    fixedTime = DateTime.Now.Ticks;
                }
                if (currentTime - lastTime > 10_000_000 / fps)
                {
                    Render();
                    lastTime = DateTime.Now.Ticks;
                }
            }
            End();
        }

        void Init()
        {
            Console.CursorVisible = false;
            isOver = false;
            isPause = false;
            speed = 3;
            fps = 30;
            Console.SetWindowSize(100, 32);
            Console.SetBufferSize(120, 60);
            SceneManager.Instance.Init();
        }

        void Input()
        {
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.P))
            {
                isPause = !isPause;
            }

            SceneManager.Instance.Input();


            //switch (scene)
            //{
            //    case Scene.Map1:
            //        hero.Input();
            //        if (InputSystem.Instance.IsKeyPressed(ConsoleKey.O))
            //        {
            //            scene = Scene.ToolMenu;
            //            toolMenu.isOpen = true;
            //            break;
            //        }
            //        break;
            //    case Scene.ToolMenu:
            //        toolMenu.Input();
            //        if (InputSystem.Instance.IsKeyPressed(ConsoleKey.O))
            //        {
            //            scene = Scene.Map1;
            //            toolMenu.isOpen = false;
            //            break;
            //        }
            //        break;
            //    case Scene.Attack:
            //        cs.Input();
            //        break;
            //}
        }

        void Update()
        {
            SceneManager.Instance.Update();

            //cs.Update();
            //hero.Update();
            //toolMenu.Update();
            //toolMenu.PrintInfo();
        }
        void FixedUpdate()
        {
            SceneManager.Instance.FixUpdate();
        }

        void Render()
        {
            SceneManager.Instance.Render();

            //InfoSystem.Instance.PrintInfo();
            //toolMenu.PrintInfo();
            //map.Render();
            //hero.Render();
            //cs.Render();
        }

        void End()
        {
            SceneManager.Instance.End();
        }
    }
}
